<template>
  <div class="mod-config">
    <basic-container>
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="人员名称：">
          <el-input v-model="searchForm.likeName" placeholder="人员名称"></el-input>
        </el-form-item>
        <el-form-item label="设备编号：">
          <el-input v-model="searchForm.deviceNumber" placeholder="设备编号"></el-input>
        </el-form-item>
        <el-form-item label="设备位置：">
          <el-input v-model="searchForm.location" placeholder="设备位置"></el-input>
        </el-form-item>
        <el-form-item label="日期选择：">
          <el-date-picker v-model="dateList" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button v-if="permissions.hik_face_add" icon="el-icon-plus" type="primary"
            @click="addOrUpdateHandle()">新增</el-button>
          <el-button v-if="permissions.hik_face_export" icon="el-icon-download" type="primary" plain
            @click="exportExcel()">导出</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getDataList()" icon="el-icon-search">查询</el-button>
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading">
          <el-table-column prop="indexCode" header-align="center" align="center" label="人脸编号">
          </el-table-column>
          <el-table-column prop="name" header-align="center" align="center" label="人脸的名称">
          </el-table-column>
          <el-table-column prop="groupName" header-align="center" align="center" label="所属组别">
          </el-table-column>
          <el-table-column prop="certificateType" header-align="center" align="center" label="证件类别">
          </el-table-column>
          <el-table-column header-align="center" align="center" label="人脸照片">
            <template slot-scope="scope">
              <img v-if="scope.row.faceUrl" :src="scope.row.faceUrl" style="float:left;width:100%;height: 100%">
            </template>
          </el-table-column>
          <el-table-column prop="deviceNumber" header-align="center" align="center" label="录入设备号">
          </el-table-column>
          <el-table-column prop="location" header-align="center" align="center" label="设备位置">
          </el-table-column>
          <el-table-column prop="sex" header-align="center" align="center" :formatter="sex" label="性别">
          </el-table-column>
          <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
          </el-table-column>
          <el-table-column prop="updateTime" header-align="center" align="center" label="更新时间">
          </el-table-column>
          <el-table-column prop="updateBy" header-align="center" align="center" label="操作人">
          </el-table-column>
          <el-table-column header-align="center" align="center" label="操作">
            <template slot-scope="scope">
              <el-button v-if="permissions.hik_face_edit" type="text" size="small" icon="el-icon-edit"
                @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
              <el-button v-if="permissions.hik_face_del" type="text" size="small" icon="el-icon-delete"
                @click="deleteHandle(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, delObj, getFaceGroup } from '@/api/hik/face'
import TableForm from './face-form'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      dataForm: {
        key: ''
      },
      doorStatus: [{
        value: '0',
        label: '允许'
      }, {
        value: '1',
        label: '不允许'
      }],
      dateList: [],
      searchForm: {},
      dataList: [],
      faceGroupList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    TableForm
  },
  //页面刚一加载就调用的方法
  created() {
    this.getDataList()
    this.getGroupDataList()


  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    // 获取数据列表
    getGroupDataList() {
      this.dataListLoading = true
      getFaceGroup(Object.assign({})).then(response => {
        console.log(response.data.data);
        this.GroupDataList = response.data.data
        console.log(this.GroupDataList)
      })
      this.dataListLoading = false
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      if (this.dateList != null && this.dateList.length > 1) {
        this.searchForm.startTime = this.dateList[0];
        this.searchForm.endTime = this.dateList[1];
      } else {
        this.searchForm.startTime = ''
        this.searchForm.endTime = ''
      }
      fetchList(Object.assign({
        current: this.pageIndex,
        size: this.pageSize
      }, this.searchForm)).then(response => {
        this.dataList = response.data.data.records
        this.totalPage = response.data.data.total
      })
      this.dataListLoading = false
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      this.$confirm('是否确认删除ID为' + id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getDataList()
      })
    },
    //  导出excel
    exportExcel() {
      this.downBlobFile('/hik/face/face/export', this.searchForm, '人脸管理模块.xlsx')
    },
    //1-男性，2-女性，UNKNOWN-未知"
    sex(row, column) {
      if (row.sex === '1') {
        return '男性'
      } else if (row.sex === '2') {
        return '女性'
      } else {
        return '未知'
      }
    },
    //111-身份证，OTHER-其它证件
    certificateType(row, column) {
      if (row.certificateType === '111') {
        return '身份证'
      } else {
        return '其它证件'
      }
    }
  }
}
</script>
